package com.alibaba.citrus.cr.mn.order.center.facade.function.impl;

import com.alibaba.citrus.cr.common.exception.FacadeException;
import com.alibaba.citrus.cr.mn.order.center.facade.function.check.CommonCheck;
import com.alibaba.citrus.cr.mn.order.center.facade.function.common.AbstractOrderBizCloseService;
import com.alibaba.citrus.cr.mn.order.center.facade.function.common.OrderAtomCloseService;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.mnordercenter.model.dto.Modifiers;
import com.epoch.app.mnordercenter.model.dto.OrderAtomBatchCloseRequest;
import com.epoch.app.mnordercenter.model.dto.OrderBizBatchCloseRequest;
import com.epoch.app.mnordercenter.orderbaserequest.OrderAtomCloseRequest;
import com.epoch.app.mnordercenter.orderbaserequest.OrderAtomCloseResponse;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author dongju
 * @date 2024/02/17
 */
@Service("salesOrderGroupCloseService")
public class SalesOrderGroupCloseServiceImpl extends AbstractOrderBizCloseService {
    private static final Log log = Log.getLogger(SalesOrderGroupCloseServiceImpl.class);

    @Resource(name = "salesOrderAtomSyncCloseService")
    private OrderAtomCloseService salesOrderAtomSyncCloseService;

    @Resource
    private CommonCheck commonCheck;

    @Override
    public List<OrderAtomCloseResponse> assembleBizBaseInfo(OrderBizBatchCloseRequest orderBizBatchCloseRequest) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("assembleBizBaseInfo");
        Map<String, Modifiers> modifierMap = commonCheck.getModifiers(Lists.newArrayList(orderBizBatchCloseRequest.getModifierCode()));
        // 人员不存在或人员状态不正确
        if (MapUtils.isEmpty(modifierMap)) {
            throw new FacadeException("OC-01-001-01-16-041", "人员不存在");
        }
        Modifiers modifier = modifierMap.get(orderBizBatchCloseRequest.getModifierCode());
        if (Objects.isNull(modifier) || modifier.getStatus() != 1) {
            throw new FacadeException("OC-01-001-01-16-041", "人员不存在");
        }
        for (OrderAtomCloseRequest orderAtomCloseRequest : orderBizBatchCloseRequest.getOrderList()) {
            orderAtomCloseRequest.setModifierName(modifier.getName());
        }
        return Lists.newArrayList();
    }

    @Override
    public Result<List<OrderAtomCloseResponse>> processAtomAbility(OrderAtomBatchCloseRequest request, List<OrderAtomCloseResponse> responses) {
        log.info("SalesOrderGroupCloseServiceImpl#processAtomAbility responses={}", responses);
        try {
            if (CollectionUtils.isNotEmpty(request.getOrderList())) {
                List<OrderAtomCloseResponse> closeResponses = salesOrderAtomSyncCloseService.batchClose(request);
                responses.addAll(closeResponses);
            }
//            boolean status = responses.stream().noneMatch(OrderAtomCloseResponse::getSuccess);
//            if (status) {
//                return Result.fail(responses, "", "");
//            }
            return Result.success(responses, "", "");
        } catch (Exception e) {
            log.error("SalesOrderGroupCloseServiceImpl#processAtomAbility error", e);
            throw e;
        }
    }
}
